import { defineStore } from 'pinia'
import router from '@/router'
import QRCode from 'qrcode'
import {
  postCreateOrderBy,
  reqCancelOrderByOrderId,
  reqGetOrderInfoById,
  reqGetStatusList,
  reqQueryOrderStatus,
  reqUserOrderList,
  reqWXPayCodeDataByOrderId
} from '@/service/modules/order'

import type {
  IOrderConfig,
  IOrderInfoData,
  IOrderStatusDataArr,
  IUserOrderListData,
  IWXPay
} from '@/service/modules/order/type'

interface IOrderState {
  orderId: number
  orderInfo: IOrderInfoData
  isCancel: boolean
  wxPayInfo: IWXPay
  ercodeImgUrl: string
  userOrderInfo: IUserOrderListData
  orderStatusArr: IOrderStatusDataArr
}
export const useOrderStore = defineStore('order', {
  state: (): IOrderState => ({
    orderId: -1,
    orderInfo: ({} as IOrderInfoData) || {},
    isCancel: false,
    wxPayInfo: {} as IWXPay,
    ercodeImgUrl: '',
    userOrderInfo: {} as IUserOrderListData,
    orderStatusArr: []
  }),
  actions: {
    async fetchPostCreateOrderByIds(
      hoscode: string,
      scheduleId: string,
      patientId: number
    ) {
      const res = await postCreateOrderBy(hoscode, scheduleId, patientId)
      // router.push({
      //   path: '/user/order',
      //   query: {
      //     orderId: 3086
      //   }
      // })
      // return
      if (res.code == 200) {
        this.orderId = res.data
        // 跳转路由
        router.push({
          path: '/user/order',
          query: {
            orderId: res.data
          }
        })
      } else {
        ElMessage.error(res.message)
      }
    },
    async fetchGetOrderInfoById(id: string) {
      const res = await reqGetOrderInfoById(id)
      if (res.code == 200) {
        this.orderInfo = res.data
      } else {
        ElMessage.error(res.message)
      }
    },
    async fetchCancelOrderById(id: string) {
      const res = await reqCancelOrderByOrderId(id)
      if (res.code == 200) {
        this.isCancel = res.data
        ElMessage.success('取消预约成功！')
      } else {
        ElMessage.error(res.message)
      }
    },
    // 微信支付二维码
    async fetchWxPayErcodeByOrderId(orderId: string) {
      const res = await reqWXPayCodeDataByOrderId(orderId)
      if (res.code == 200) {
        this.wxPayInfo = res.data
        this.ercodeImgUrl = await QRCode.toDataURL(res.data.codeUrl)
        // 查询订单支付状态
      }
    },
    // 询问订单支付状态
    async fetchPayResultStatusById(orderId: string) {
      const res = await reqQueryOrderStatus(orderId)
      if (res.code == 200) {
        return res
      } else {
        ElMessage.error(res.message)
      }
    },
    // 订单列表
    async fetchUserOrderListAction(config: IOrderConfig) {
      const res = await reqUserOrderList(config)
      if (res.code == 200) {
        this.userOrderInfo = res.data
      } else {
        ElMessage.error('用户订单列表获取失败！')
      }
    },
    // 订单状态
    async fetchOrderStatusAction() {
      const res = await reqGetStatusList()
      if (res.code == 200) {
        this.orderStatusArr = res.data
      }
    }
  }
})
